package com.xfpp.javaDemo.iterator;

import com.xfpp.javaDemo.iterator.Collection;

public class ArrayList implements Collection{
	private Object[] objs = new Object[10];
	private int index = 0;
	
	public void add(Object o) {
		if(index == objs.length) {
			Object[] newObjs = new Object[objs.length * 2];
			System.arraycopy(objs, 0, newObjs, 0, objs.length);
			objs = newObjs;
		}
		objs[index] = o;
		index++;
	}
	
	public int getSize() {
		return index;
	}

	@Override
	public Iterator iterator() {
		return new ArrayListIterator();
	}
	
	private class ArrayListIterator implements Iterator {
		private int currentIndex = 0;
		
		@Override
		public Object next() {
			Object o = objs[currentIndex];
			currentIndex ++;
			return o;
		}

		@Override
		public boolean hasNext() {
			if(currentIndex >= index) {
				return false;
			}
			return true;
		}
	}
}
